VERSION 1.0 CLASS
BEGIN
  MultiUse = -1  'True
  Persistable = 0  'NotPersistable
  DataBindingBehavior = 0  'vbNone
  DataSourceBehavior  = 0  'vbNone
  MTSTransactionMode  = 0  'NotAnMTSObject
END
Attribute VB_Name = "clsIPBans"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = True
Attribute VB_PredeclaredId = False
Attribute VB_Exposed = False
Attribute VB_Ext_KEY = "SavedWithClassBuilder6" ,"Yes"
Attribute VB_Ext_KEY = "Top_Level" ,"Yes"
'-----------------------------------------------------------------
'             PT DC Hub @ Direct Connect P2P Network
'-----------------------------------------------------------------
'       Developer: Carlos.DF (fLaSh) - Portugal
'          E-mail: carlosferreiracarlos@hotmail.com
' Project started: 10 - September - 2006
'         License: GNU General Public License.
'-----------------------------------------------------------------
'       Thanks to developers and contributores of SDCH/DDCH
'         The Left Hand, ButterflySoul, HaArD and Selyb
'  TheNOP, RollTheDice, JDommi, GhOstFaCE, ArchaicLight and TUFF
'-----------------------------------------------------------------
Option Explicit

Private m_colTemp       As Collection
Private m_colPerm       As Collection
Private m_colRange      As Collection

Private m_objData       As clsIPBansData

Private Sub Class_Initialize()
1:    Set m_colTemp = New Collection
2:    Set m_colPerm = New Collection
3:    Set m_colRange = New Collection
End Sub

Private Sub Class_Terminate()
1:    Set m_colTemp = Nothing
2:    Set m_colPerm = Nothing
3:    Set m_colRange = Nothing
End Sub

'Ban a new IP
'varMinutes = -1 = Perm ban
'           > 0  = Temp ban
'           = Valid IP = Perm ban range
'bHidePopUp: used only at LoadSettings (hide popup ballon)
Public Sub Add(ByRef strIP As String, Optional ByRef varMinutes As Variant = -1, Optional ByRef strNick As String = Empty, Optional ByRef strBannedBy As String = Empty, Optional strReason As String = Empty, Optional bHidePopUp As Boolean = False)
1:    On Error GoTo Err
2:    Dim strTemp As String
    
       'Ban IP Range =================================================================================================================================================================================================================================
5:     If g_objMyIPTools.ValidIP(varMinutes) Then
    
           'Check if Start IP is < End IP
8:         If Not g_objMyIPTools.ValidRange(strIP, CStr(varMinutes)) Or _
                    CheckRange(strIP, CStr(varMinutes)) Then 'Check in collection
10:            Exit Sub
11:        End If
        
13:        Set m_objData = New clsIPBansData
        
15:        m_objData.IP = strIP
16:        m_objData.Nick = CStr(varMinutes)
17:        m_objData.BannedBy = strBannedBy
18:        m_objData.Reason = strReason
           '
20:        m_colRange.Add m_objData, CStr(strIP & " " & CStr(varMinutes))
           '
           'Run script event
23:        frmHub.SEvent_AddedPermBan strIP, CStr(varMinutes), strBannedBy, strReason
           '
25:        Set m_objData = Nothing
           '
27:        If Not bHidePopUp Then
28:            strTemp = "IP Range Ban" & vbNewLine & _
            "Start IP: " & strIP & vbNewLine & _
            "End IP: " & CStr(varMinutes) & vbNewLine & _
            "Banned by: " & strBannedBy & vbNewLine & _
            "Reason: " & strReason
            '
            'Show Ballon notification
35:            If g_objSettings.PopUpRangeBan Then _
                    g_objFunctions.ShowBallon "PT DC Hub " & vbVersion, _
                    g_objSettings.HubName & vbNewLine & _
                    strTemp, 1, True
               '
40:            AddLog strTemp
41:        End If
42:    Else
        'Ban IP Perm =================================================================================================================================================================================================================================
        'Select right ban type
45:        If CInt(varMinutes) = -1 Then
46:            If Not CheckPerm(strIP) Then
47:                Set m_objData = New clsIPBansData
48:                m_objData.IP = strIP
49:                m_objData.Nick = strNick
50:                m_objData.BannedBy = strBannedBy
51:                m_objData.Reason = strReason
                   '
53:                m_colPerm.Add m_objData, strIP
                   '
                   'Run script event
56:                frmHub.SEvent_AddedPermBan strIP, strNick, strBannedBy, strReason
57:                Set m_objData = Nothing
                   '
59:                If Not bHidePopUp Then
60:                    strTemp = "IP Kicked Permanent" & vbNewLine & _
                                "Nick: " & strNick & vbNewLine & _
                                "IP: " & strIP & vbNewLine & _
                                "Banned by: " & strBannedBy & vbNewLine & _
                                "Reason: " & strReason
                    '
                    'Show Ballon notification
67:                    If g_objSettings.PopUpUserKick Then _
                            g_objFunctions.ShowBallon "PT DC Hub " & vbVersion, _
                            g_objSettings.HubName & vbNewLine & _
                            strTemp, 1, True
                       '
72:                    AddLog strTemp
73:                End If
74:            End If
75:        Else
            'Ban IP Temp =================================================================================================================================================================================================================================
            'If already banned, then replace old ban time with new
78:            If CheckTemp(strIP) Then
79:                m_colTemp(strIP).ExpDate = DateAdd("n", CInt(varMinutes), CheckDate(m_colTemp(strIP).ExpDate))
80:            Else
81:                Set m_objData = New clsIPBansData
82:                m_objData.IP = strIP
83:                m_objData.ExpDate = DateAdd("n", CInt(varMinutes), Now)
84:                m_objData.Nick = strNick
85:                m_objData.BannedBy = strBannedBy
86:                m_objData.Reason = strReason
                '
88:                m_colTemp.Add m_objData, strIP
                '
                'Run script event
91:                frmHub.SEvent_AddedTempBan strIP, CInt(varMinutes), strNick, strBannedBy, strReason
                '
93:                Set m_objData = Nothing
                '
95:                If Not bHidePopUp Then
96:                    strTemp = "IP Kicked Temporary" & vbNewLine & _
                                "Nick: " & strNick & vbNewLine & _
                                "IP: " & strIP & vbNewLine & _
                                "Time: " & varMinutes & " minute(s)" & vbNewLine & _
                                "Banned by: " & strBannedBy & vbNewLine & _
                                "Reason: " & strReason
                    '
                    'Show Ballon notification
104:                    If g_objSettings.PopUpUserKick Then _
                        g_objFunctions.ShowBallon "PT DC Hub " & vbVersion, _
                        g_objSettings.HubName & vbNewLine & _
                        strTemp, 1, True
                    '
109:                    AddLog strTemp
110:                End If
111:            End If
112:        End If
113:    End If

115:    Exit Sub
    
117:
Err:
118:    HandleError Err.Number, Err.Description, Erl & "|" & "clsIPBans.Add(""" & strIP & """, " & varMinutes & ")"
End Sub

'Remove an IP or Range
'intType = 0 = Bot
'        = 1 = Temp
'        = 2 = Perm
'        = 3 = Range
'varEndIP = useded only for ban range
Public Sub Remove(ByRef strIP As String, Optional ByRef varEndIP As Variant = Empty, Optional ByRef intType As Integer = 0)
1:    On Error GoTo Err
 
     'This line is to be compatible old scripts..
     Select Case TypeName(varEndIP)
        Case "Long", "Integer"
4:            varEndIP = intType
5:    End Select

     'Select which ban lists we should check/remove
      Select Case intType
        Case 0
8:            If CheckTemp(strIP) Then m_colTemp.Remove strIP
9:            If CheckPerm(strIP) Then m_colPerm.Remove strIP
10:           If CheckRange(strIP, CStr(varEndIP)) Then m_colRange.Remove CStr(strIP & " " & CStr(varEndIP))
        Case 1
11:           If CheckTemp(strIP) Then m_colTemp.Remove strIP
        Case 2
12:           If CheckPerm(strIP) Then m_colPerm.Remove strIP
        Case 3 'For rem range strIP = "StartIp EndIP"
14:           If CheckRange(strIP, CStr(varEndIP)) Then m_colRange.Remove CStr(strIP & " " & CStr(varEndIP))
15:    End Select

17:    Exit Sub
    
19:
Err:
20:    HandleError Err.Number, Err.Description, Erl & "|" & "clsIPBans.Remove(""" & strIP & """)"
End Sub

'Check if the ips are banned
'   0    = Not banned
'   -1   = Perm banned
'   -2   = Perm range banned
'   Else = Temp banned (length in minutes)
Public Function Check(ByRef strIP As String) As Long
1:     On Error GoTo Err

3:     If CheckPerm(strIP) Then
4:        Check = -1
5:     ElseIf CheckTemp(strIP) Then
          'Check if the date has expired
7:        Check = DateDiff("n", Now, m_colTemp(strIP).ExpDate)
          'If the date is earlier than the current date, the temp ip ban
          'has expired
10:        If Check < 1 Then
11:            m_colTemp.Remove strIP
12:            Check = 0
13:        End If
14:    ElseIf IsIP_InRange(strIP) Then
15:           Check = -2
16:    End If
    
18:    Exit Function
    
20:
Err:
21:    HandleError Err.Number, Err.Description, Erl & "|" & "clsIPBans.Check(""" & strIP & """)"
End Function

Public Function GetRangesInIP(ByVal sIP As String) As String
2:    On Error GoTo Err
      
      'Return format: "StartIP EndIP", ie: "127.125.125.125 255.255.255.255"
      'Don't loop if there is nothing in the collection
5:    If m_colRange.count Then
6:         For Each m_objData In m_colRange
7:            If g_objMyIPTools.IPinRange(sIP, CStr(m_objData.IP), CStr(m_objData.Nick)) Then
8:                 GetRangesInIP = GetRangesInIP & _
                          m_objData.IP & " " & m_objData.Nick & vbNewLine
9:            End If
10:        Next
12:    End If

14:   Exit Function
    
16:
Err:
17:   HandleError Err.Number, Err.Description, Erl & "|" & "clsIPBans.GetRanges(" & sIP & ")"
End Function

Public Sub RemRangesInIP(ByVal sIP As String)
2:    On Error GoTo Err
    
      'Don't loop if there is nothing in the collection
5:    If m_colRange.count Then
6:         For Each m_objData In m_colRange
7:            If g_objMyIPTools.IPinRange(sIP, CStr(m_objData.IP), CStr(m_objData.Nick)) Then
8:                 m_colRange.Remove (CStr(m_objData.IP) & " " & CStr(m_objData.Nick))
9:            End If
10:        Next
12:   End If

14:   Exit Sub
    
16:
Err:
17:   HandleError Err.Number, Err.Description, Erl & "|" & "clsIPBans.RemRangesInIP(" & sIP & ")"
End Sub

'Clear temp ban list
Public Sub ClearTemp()
1:    Set m_colTemp = New Collection
End Sub

'Clear perm ban list
Public Sub ClearPerm()
1:    Set m_colPerm = New Collection
End Sub

'Clear perm ban range list
Public Sub ClearRange()
1:    Set m_colRange = New Collection
End Sub

'Loop through listitems to get a list of temp banned ips (with expiration date)
Public Function TempList() As String
1:    On Error GoTo Err
    
      'Don't loop if there is nothing in the collection
4:    If m_colTemp.count Then
5:        For Each m_objData In m_colTemp
6:            TempList = TempList & "|" & m_objData.IP & "|" & m_objData.ExpDate & "|" & m_objData.Nick & "|" & m_objData.Reason & "|" & m_objData.BannedBy & "|" & vbNewLine
7:        Next
        
9:       Set m_objData = Nothing
10:   End If

12:    Exit Function
    
14:
Err:
15:    HandleError Err.Number, Err.Description, Erl & "|" & "clsIPBans.TempList()"
End Function

'Loop through listitems to get a list of perm banned ips
Public Function PermList() As String
1:    On Error GoTo Err
    
      'Don't loop if there is nothing in the collection
4:    If m_colPerm.count Then
5:        For Each m_objData In m_colPerm
6:            PermList = PermList & "|" & m_objData.IP & "|" & m_objData.Nick & "|" & m_objData.Reason & "|" & m_objData.BannedBy & "|" & vbNewLine
7:        Next
8:   End If

10:   Exit Function
    
12:
Err:
13:    HandleError Err.Number, Err.Description, Erl & "|" & "clsIPBans.PermList()"
End Function

'Loop through listitems to get a list of range perm banned ips
Public Function RangeList() As String
1:    On Error GoTo Err
    
      'Don't loop if there is nothing in the collection
4:    If m_colRange.count Then
5:        For Each m_objData In m_colRange
6:            RangeList = RangeList & "|" & m_objData.IP & "|" & m_objData.Nick & "|" & m_objData.Reason & "|" & m_objData.BannedBy & "|" & vbNewLine
7:        Next
8:    End If

10:   Exit Function
    
12:
Err:
13:    HandleError Err.Number, Err.Description, Erl & "|" & "clsIPBans.RangeList()"
End Function

Public Property Get TempItems() As Collection
1:    Set TempItems = m_colTemp
End Property

Public Property Get PermItems() As Collection
1:    Set PermItems = m_colPerm
End Property

Public Property Get RangeItems() As Collection
1:    Set RangeItems = m_colRange
End Property

Private Function CheckDate(ByRef datCheck As Date) As Date
1:    If DateDiff("n", datCheck, Now) Then
2:        CheckDate = Now
3:    Else
4:        CheckDate = datCheck
5:    End If
End Function

Private Function CheckPerm(ByRef strIP As String) As Boolean
1:    On Error GoTo Err
    
3:    m_colPerm.Item strIP
4:    CheckPerm = True

6:    Exit Function
7:
Err:
End Function

Private Function CheckTemp(ByRef strIP As String) As Boolean
1:    On Error GoTo Err
    
3:    m_colTemp.Item strIP
4:    CheckTemp = True

6:    Exit Function
7:
Err:
End Function

Private Function CheckRange(ByRef strStartIP As String, ByRef strEndIP As String) As Boolean
1:    On Error GoTo Err
    
3:    m_colRange.Item CStr(strStartIP & " " & strEndIP)
4:    CheckRange = True

6:    Exit Function
7:
Err:
End Function

Private Function IsIP_InRange(ByRef strIP As String) As Boolean
1:    Dim objTB          As clsIPBansData
2:    On Error GoTo Err

4:    If Not g_objMyIPTools.ValidIP(strIP) Then
5:        IsIP_InRange = False
6:        Exit Function
7:    End If

9:     For Each objTB In m_colRange
10:        If g_objMyIPTools.ValidIP(objTB.IP) And g_objMyIPTools.ValidIP(objTB.Nick) Then
11:           If g_objMyIPTools.IPinRange(strIP, CStr(objTB.IP), CStr(objTB.Nick)) Then
12:               IsIP_InRange = True
13:               Exit For
14:           End If
15:        End If
16:    Next

18:    Set objTB = Nothing
       
20:    Exit Function
21:
Err:
22:    HandleError Err.Number, Err.Description, Erl & "|" & "clsIPBans.IsIP_InRange(""" & strIP & """)"
End Function

Private Sub RemoveRange(ByRef strStartIP As String, ByRef strEndIP As String)
1:    On Error GoTo Err
      'Select which ban range lists we should check/remove
3:    If CheckRange(strStartIP, strEndIP) Then m_colRange.Remove CStr(strStartIP & " " & strEndIP)
      
5:    Exit Sub
6:
Err:
7:    HandleError Err.Number, Err.Description, Erl & "|" & "clsIPBans.RemoveRange(" & strStartIP & ", " & strEndIP & ")"
End Sub
